/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

/**
 *
 * @author dark
 */
import Control.SemanticQualificationController;
import Model.Correspondence;
import Model.FormalConcept;
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;  
import javax.swing.JToggleButton;  
import javax.swing.SwingConstants;  

public class MyMenuButton extends JToggleButton{  
    private JPopupMenu menu = new JPopupMenu();  
    private static int i = 0;  
    private FormalConcept fc;
    private PrincipalWindowGUI pwg;
    
    public MyMenuButton(){  
        super();  
        this.initMenu();
        this.setText("▲");  
        this.setHorizontalTextPosition(SwingConstants.RIGHT );  
        addActionListener(new ActionListener(){  
            @Override  
            public void actionPerformed(ActionEvent arg0) {  
                if(isSelected()){  
                    setText("▼");  
                    menu.show(MyMenuButton.this, 0, MyMenuButton.this.getHeight());  
                }else{  
                    setText("▲");  
                    menu.setVisible(false);  
                }  
            }  
        });  
    }  
    public MyMenuButton(final String label, FormalConcept myFC){  
        super(label); 
        this.initMenu();
        this.setFC(myFC);
        this.setText("▲ "+label);  
        this.setHorizontalTextPosition(SwingConstants.RIGHT );  
        addActionListener(new ActionListener(){  
            @Override  
            public void actionPerformed(ActionEvent arg0) {  
                if(isSelected()){  
                    setText("▼"+label);  
                    menu.show(MyMenuButton.this, 0, MyMenuButton.this.getHeight());  
                }else{  
                    setText("▲"+label);  
                    menu.setVisible(false);  
                }  
            }  
        });  
    }  
    
    public void setMenu(JPopupMenu menu){  
        this.menu=menu;  
    }
    
    public void setPWG(PrincipalWindowGUI gui){
        this.pwg = gui;
    }
    
    class PopupActionListener implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + actionEvent.getActionCommand());
            // open the window NewCP
            NewCP cp = new NewCP(pwg, fc);
            cp.setVisible(true); 
            ActionListener actionListener = new CPConfirmActionListener();
            cp.getJButtonCPConfirm().addActionListener(actionListener);
        }
    }
    
    class CPConfirmActionListener implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("CP CONFIRM!!");
        }
    }
    
    public void initMenu(){
        JMenuItem addCPitem = new JMenuItem("+");
        ActionListener actionListener = new PopupActionListener();
        addCPitem.addActionListener(actionListener);
        menu.add(addCPitem);
    }
    
    public void updateMenu(List<String> cps){
        menu.removeAll();
        this.initMenu();
        for (int i = 0; i < cps.size(); i++){
            menu.add(cps.get(i));
        }
    }
    
    public void setFC(FormalConcept myFC){
        System.out.println("FC name "+myFC.getName());
        fc = myFC;
    }
}  
